package com.zxw.order.init;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 初始化加载信息
 */
@Component
@Slf4j
public class InitResourceRoleURLRunner implements CommandLineRunner {

    @Value("${load.url.to.add.to.Redis}")
    private Boolean loadUrlToAddToReadis;

    @Value("${load.url.to.add.to.MySQL}")
    private Boolean loadUrlToAddToMySQL;

    @Resource
    private WebApplicationContext applicationContext;

    @Override
    public void run(String... args) {
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 拿到Handler适配器中的全部方法
        Map<RequestMappingInfo, HandlerMethod> methodMap = mapping.getHandlerMethods();
        List<String> urlList = new ArrayList<>();
        for (RequestMappingInfo info : methodMap.keySet()) {

            Set<String> urlSet = info.getPatternsCondition().getPatterns();
            // 获取全部请求方式
            Set<RequestMethod> Methods = info.getMethodsCondition().getMethods();
            System.out.println(Methods.toString());
            for (String url : urlSet) {
                // 加上自己的域名和端口号，就可以直接调用
                System.out.println("url:" + url);
                urlList.add(url);
            }
        }
        if (loadUrlToAddToReadis) {
            System.out.println(1);
        }

        if (loadUrlToAddToMySQL) {
            System.out.println(11);
        }
    }
}
